﻿
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;

using System.Security.Claims;
using System.Text;
using System.Web;


namespace WebFile.Models
{

    public static class LoginMode
    {
        private static IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

        private static JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();

        public static void Configure(IHttpContextAccessor accessor)
        {
            httpContextAccessor = accessor;
        }

        public static string username
        {
            get
            {
                if (GetClaimValue(Token, ClaimTypes.Name, LoginLX.下线) != null)
                {
                    return GetClaimValue(Token, ClaimTypes.Name, LoginLX.下线);
                }
                else
                {
                    return null;
                }
            }
            set
            {
                SetSessionValue(KeyValueConfig.KeyUserSession, value);
            }
        }

        public static string Token
        {
            get
            {
                return (string)GetCookieValue(KeyValueConfig.Cookie_Token);
            }
            set
            {
                SetCookieValue(KeyValueConfig.Cookie_Token, value);
            }
        }

        public static string LV
        {
            get
            {
                return GetClaimName(Token);
            }
            set
            {
                GetClaimName(Token);
            }
        }

        public static bool admin
        {
            get
            {
                return GetAdmin(Token);
            }
            set
            {
                GetAdmin(Token);
            }
        }

        private static void SetSessionValue(string name, object value)
        {
            var currentHttpContext = httpContextAccessor.HttpContext;
            if (currentHttpContext != null)
            {
                currentHttpContext.Session.SetString(name, value.ToString());
            }

        }

        private static object GetSessionValue(string name)
        {
            var currentHttpContext = httpContextAccessor.HttpContext;
            if (currentHttpContext != null)
            {
                var session = currentHttpContext.Session;
                return session == null ? default : session.GetString(name);
            }
            else
            {
                return null;
            }
        }

        private static void SetCookieValue(string name, object value)
        {
            var currentHttpContext = httpContextAccessor.HttpContext;
            if (currentHttpContext != null)
            {
                currentHttpContext.Response.Cookies.Append(name, value.ToString());
            }

        }

        private static object GetCookieValue(string name)
        {
            var currentHttpContext = httpContextAccessor.HttpContext;
            if (currentHttpContext != null)
            {
                var cookie = currentHttpContext.Request.Cookies[name];
                if (cookie != null)
                {
                    return cookie;
                }
            }
            else
            {

                return null;
            }

            return null;
        }

        private static string GetClaimName(string token)
        {
            var tokenName = GetClaimValue(token, ClaimTypes.Name);
            if (tokenName != null)
            {
                return tokenName;
            }
            return null;
        }

        private static string GetClaimValue(string token, string claimType, LoginLX LX = LoginLX.上线)
        {
            var jwt = tokenHandler.ReadJwtToken(token);
            if (jwt.ValidTo >= DateTime.UtcNow)
            {
                if (claimType.Equals(ClaimTypes.Name))
                {
                    if (LX == LoginLX.下线)
                    {
                        return jwt.Claims.FirstOrDefault(w => w.Type == claimType)?.Value;
                    }
                    else
                    {
                        return jwt.Claims.FirstOrDefault(w => w.Type == claimType && !w.Value.Equals(username))?.Value;
                    }
                }
                else
                {
                    return jwt.Claims.FirstOrDefault(w => w.Type == claimType && w.Value.Equals("admin"))?.Value;
                }
            }
            return null;
        }

        private static bool GetAdmin(string token)
        {
            var tokenName = GetClaimValue(token, ClaimTypes.Role);
            if (tokenName != null)
            {
                return tokenName.Equals("admin");
            }
            return false;
        }

        enum LoginLX
        {
            下线,
            上线
        }
    }
}
